﻿<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML >
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport"
			content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="renderer" content="webkit">
		<title>购物车</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/admin/css/pintuer.css">
		<script src="<%=request.getContextPath()%>/admin/js/jquery.js"></script>
		<script src="<%=request.getContextPath()%>/admin/js/pintuer.js"></script>

		<script src="<%=request.getContextPath()%>/admin/js/admin.js"></script>
	</head>
	<body>
		
		<s:if test="order0s.size==0">
			<div>
				<div>
					<img width="100%" alt="" src="<%=path %>/w/icon/icon-gwc.png">
				</div>
				<div style="text-align: center; font-size: 2em;">
					购物车还是空的
				</div>
				<div style="text-align: center; font-size: 2em;" class="margin">
					<a href="<%=path %>product!findallp.com.kecheng.action">
						<button class="button bg-yellow" style="width: 80%;">
							去逛逛
						</button> </a>
				</div>
			</div>
		</s:if>
		<s:else>
			<div class="container">
				<s:iterator value="order0s">
					<div class="drug " style="margin-top: 15px;">
						<div class="line-big">
							<div class="x1">
								<input name="checkbox" value="<s:property value="cid"/>"
									type="checkbox" class="choose" onchange="updateAll();" />
							</div>
							<div class="x3">
								<div id="img">
									<img height="60" width="60"
										src="<%=path%>/<s:property value="product.img"/>" />
								</div>
							</div>
							<div class="x7">
								<div>
									<div class="x12" style="text-align: left;">
										<s:property value="product.title" />
									</div>
									<div style="clear: both;"></div>
								</div>
								<div>	
									<div class="x8">
									单价:
									<font color="red"><strong>￥<SPAN class="price"><s:property
													value="product.price" /> </SPAN> </strong> </font>
									</div>
									<div style="clear: both;"></div>
								</div>
								<div>
									<div class="x7">
									积分:
									<font color="red"><strong><SPAN class="score"><s:property
													value="product.score" /> </SPAN> </strong> </font>
									</br>
									<s:property value="guige1value" />&nbsp;<s:property value="guige2value" />&nbsp;<s:property value="guige3value" />
									</div>
									<div class="x5">
										<a href="<%=request.getContextPath()%>/w/cart!del.action?order0.cid=<s:property value="cid"/>" >
											<button type="button"
												class="button button-little border-blue margin-left">
												删除
											</button> </a>
									</div>
									<div style="clear: both;"></div>
								</div>
							</div>
						</div>

						<div style="margin: 10px 0px;">
							<div class="x1"></div>
							<div class="x3 margin-right">
								总价￥
								<span class="sum" style="color: red;"></span>
							</div>
							<div class="x7 margin-left">
								总积分
								<span class="jifen" style="color: red;"></span>
							</div>
							<div style="clear: both;"></div>
						</div>

						<div class="line margin-left " style="margin-top:5px; ">
							<div class="x2">
								<h4>
									数量:
								</h4>
							</div>
							<div class="x1">
								<button type="button" class="button button-small"
									onclick="min(this);" >
									<strong>-</strong>
								</button>
							</div>
							<div class="x2">
								<input name="num" type="text" style="background-color: white;height:100%; width: 80%;position:relative; left:10%;"
									class="input input-small num" value="<s:property value="num"/>"
									readonly="readonly" />
							</div>
							<div class="x1">
								<button type="button" class="button button-small"
									onclick="add(this);">
									<strong>+</strong>
								</button>
							</div>
							<div style="clear: both;"></div>
						</div>
						<div style="clear: both;"></div>
					</div>
				</s:iterator>
			</div>

			<div class="line-middle margin-top">
				<div class="x2">
				</div>
				<div class="x8">
					<div class="x6">
						总价￥
						<strong><font color="red" id="sum"><span
								class="all"></span> </font> </strong>
					</div>
					<div class="x6">
						总积分
						<strong><font color="red" id="score"><span></span>
						</font> </strong>
					</div>
					<div style="clear: both;"></div>
				</div>
				<div class="x2"></div>
			</div>

			<div
				style="position: fixed; width: 70%; height: 40px; left: 0px; bottom: 50px; background-color: white; line-height: 40px;">
				<h3>
					<div style="color: red;">
						&nbsp;&nbsp;&nbsp;
						<input class="allselect" type="checkbox" value="checkbox">
						全选&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				</h3>
			</div>

			<a href="javascript:void(0)" onclick="buy()">
				<div
					style="position: fixed; width: 30%; height: 40px; right: 0px; bottom: 50px; background-color: red; text-align: center; line-height: 40px; color: white;">
					<h3 style="text-align: center;">
						结算
					</h3>
				</div> </a>
		</s:else>



		<!--底部菜单开始-->
		<input type="hidden" name="" value="${team.openid }" id="openid"/>
		<s:include value="/w/tmpfoot.jsp"></s:include>

		<script type="text/javascript">  
	$().ready(function(){
	$(".allselect").bind("click",function (){
			$choose=$(".choose");
			console.log($choose);
			console.log($(this).is(":checked"));
			if($(this).is(":checked")){
				$choose.prop("checked",true);
			}else{
				$choose.prop("checked",false);
			}
			updateAll();
		});  
});                                              
</script>
		<script type="text/javascript">
			//减1
			function min(bt){
				var num= $(bt).parents(".drug").find(".num").val();
				if(--num<1){
				 	num=1;
				}
				$(bt).parents(".drug").find(".num").val(num);
				update(bt);
				updateAll();
			}
			//增1
			function add(bt){
				var num= $(bt).parents(".drug").find(".num").val();
				$(bt).parents(".drug").find(".num").val(++num);
				update(bt);
				updateAll();
			}
			//更新局部
			function update(bt){
				var price=$(bt).parents(".drug").find(".price").html();
				var score=$(bt).parents(".drug").find(".score").html();
				var num=$(bt).parents(".drug").find(".num").val();
				
				$(bt).parents(".drug").find(".jifen").html(score*num);
				$(bt).parents(".drug").find(".sum").html(price*num);
			}
			//更新全部
			function updateAll(){
				var all=0;
				var score=0;
				var $drug=$(".drug");
				for(var i=0;i<$drug.length;i++){
					console.log($drug.eq(i).find(":checkbox").is(":checked"));
					if($drug.eq(i).find(":checkbox").is(":checked")){
						all+=$drug.eq(i).find(".sum").html()-0;
						score+=$drug.eq(i).find(".jifen").html()-0;
					}
				}
				$(".all").html(all);
				$("#score").html(score);
			}
		
	
			//购买	
			function buy(){
				var $drug=$(".drug");
				var ids= "";
				var nums="";
				var openid=$('#openid').val();
				var ok=false;
				for(var i=0;i<$drug.length;i++){
					if($drug.eq(i).find(":checked").is(":checked")){
						ok=true;
						ids+=$drug.eq(i).find(":checked").val()+",";
						nums+=$drug.eq(i).find(".num").val()+",";
					}
				}
				if(!ok){
					alert("未选中");
					return;
				}
				$.ajax({
					url:"<%=path%>/w/cart!jiesuan.action",
					type:"post",
					dataType:"json",
					data:{"ids":ids,"nums":nums,"team.openid":openid},
					success:function(data){
						if(data=="操作错误"){
							alert("请稍后重试");
						}else{
							window.location.href="<%=path%>/w/goumaixinxi.jsp";
						}			
					},
					error:function(){
						alert("请稍后重试!");
					}
				});
			}	
		</script>

		<script type="text/javascript">
			var $nums=$(".num");
			for(var i=0;i<$nums.length;i++){
				update($nums[i]);
			}
			updateAll();
		</script>
	</body>
</html>
